﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenHardwareMonitor.Hardware;

namespace fancontrol
{
    public partial class Form1 : Form
    {
        Comport.Comport myComport = new Comport.Comport();
        Computer myComputer = new Computer();
        bool aan = false;
        
        public Form1()
        {
            InitializeComponent();
            this.cpu_temp.ReadOnly = true;
            myComputer.Open();
            myComputer.GPUEnabled = true;
            myComputer.CPUEnabled = true;
            myComputer.HDDEnabled = true;
            this.timer1.Start();
            //test
            
        }
        private void GetTemps()
        {
            var temps = new List<decimal>();
            float cpuTemp = 40;
            float gpuTemp = 40;
            float hdTemp = 40;
            float ramTemp = 50;
            float mainboardTemp = 40;

            foreach (var hardwareItem in myComputer.Hardware)
            {

                if (hardwareItem.HardwareType == HardwareType.GpuAti)
                {
                    foreach (var sensor in hardwareItem.Sensors)
                    {
                        if (sensor.SensorType == SensorType.Temperature)
                        {
                            gpuTemp = (float)sensor.Value;
                        }
                    }
                }
                if (hardwareItem.HardwareType == HardwareType.CPU)
                {
                    foreach (var sensor in hardwareItem.Sensors)
                    {
                        if (sensor.SensorType == SensorType.Temperature)
                        {
                            cpuTemp = (float)sensor.Value;
                        }
                    }
                }
                if (hardwareItem.HardwareType == HardwareType.HDD)
                {
                    foreach (var sensor in hardwareItem.Sensors)
                    {
                        if (sensor.SensorType == SensorType.Temperature)
                        {
                            if (sensor.Value != null)
                                hdTemp = (float)sensor.Value;
                        }
                    }
                }
            }
            this.cpu_temp.Text = +cpuTemp+ "\u00B0C";
            this.gpu_temp.Text = +gpuTemp + "\u00B0C";
            this.gpu_temp.Text = +hdTemp + "\u00B0C";
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.GetTemps();
            this.timer_aan.Text = aan.ToString();
            if (aan)
                aan = false;
            else
                aan = true;
        }
    }
}
